$(function () {
    $("#calendar").fullCalendar({
        width: 1000,
        // height: 600,
        weekMode: "variable",
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month'
        },
        weekends: true,
        dayClick: function (date, jsEvent, views) {//date为当前的时间
            $("#dialog").dialog("open");
        },
        //从后台拿到数据并设置start,end然后再通过便签的形式显示在前端日历上面
        events: function (start, end, timezone, callback) {
            var datestart = $.fullCalendar.formatDate(start, "yyyy-MM-dd");
            var dateend = $.fullCalendar.formatDate(end, "yyyy-MM-dd");
            $.ajax({
                url: '/calendarPlan/selectAll.do',
                type: 'post',
                dataType: 'json',
                contentType: 'application/json;charset:utf-8',
                cache: false,
                data: {
                    start: datestart,
                    end: dateend
                },
                success: function (result) {//result为返回值
                    var events = [];
                    for (var i = 0; i < result.length; i++) {
                        events.push({
                            title: result[i].title,
                            start: new Date(result[i].startDate),
                            end: new Date(result[i].endDate),
                            id: result[i].id
                        });
                    }
                    callback(events);
                }
            });
        },
        eventClick: function (event, jsEvent, view) {
            var id = event.id;
            $.get("/calendarPlan/selectById.do", {id: id}, function (data) {
                // $("#dialog").dialog("title", "编辑");
                $("#dialog").form("load", data);
                $("#dialog").dialog("open");
            })
        },
    });
    $("#delete").click(function () {
        var id = $("#id").val();
        $.messager.confirm("温馨提示", "确定要删除这一项日程安排吗???", function (r) {
            if (r) {
                $.get("/calendarPlan/delete.do", {id: id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "删除成功咯~", "info", function () {
                                window.location.reload();
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }
                )
            }
        })
    })

    $("#dialog").dialog({
        width: 300,
        height: 340,
        title: "新增/编辑",
        closed: true,
        buttons: "#buttons",
        onClose: function () {
            $("#editForm").form("clear");
        }
    })
});

function add() {
    $("#dialog").dialog("open");
}

function submits() {
    $("#editForm").form('submit', {
        url: "/calendarPlan/saveOrUpdate.do",
        success: function (data) {//data为返回值
            data = $.parseJSON(data);
            if (data.success) {
                $.messager.alert("温馨提示", "提交成功", "info", function () {
                    $("#dialog").dialog("close");
                    window.location.reload();
                });
            } else {
                $.messager.alert("温馨提示", data.msg, "info");
            }
        }
    })
}

function cancel() {
    $("#dialog").dialog("close");
}